package jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * 执行关联查询
 */
public class JDBCDemo6 {
    public static void main(String[] args) {
        try (Connection con = DBUtil.getConnection();){
            Statement statement = con.createStatement();
            String sql = "SELECT t.name,t.gender,c.name,c.floor " +
                         "FROM teacher t,class c " +
                         "WHERE t.id=c.teacher_id " +
                         "AND t.name='王克晶'";
            ResultSet rs = statement.executeQuery(sql);
            while (rs.next()) {
//                String name = rs.getString("t.name");
//                String gender = rs.getString("t.gender");
//                String className = rs.getString("c.name");
//                int floor = rs.getInt("c.floor");
                //也支持通过字段下标获取，注意:下标从1开始。
                String name = rs.getString(1);
                String gender = rs.getString(2);
                String className = rs.getString(3);
                int floor = rs.getInt(4);

                System.out.println(name + ", " + gender + ", " + className + ", " + floor);
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
